package com.zs.dao;

import com.zs.entity.Admin;
import com.zs.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    //调用DBUtil的getConnection()方法的到数据库的链接
    Connection conn = DBUtil.getConnection();
    PreparedStatement psmpt = null;//创建全局变量,方便所有增删改等操作的进行
    ResultSet rs = null;//创建ResultSet全局变量,接收执行sql语句后的结果集
    Admin admin = null;//创建
    public Admin adminlogin(String aname,String apwd){
        String sql = "select * from t_admin where aname = ? and apwd = ?";
        try{
            psmpt = conn.prepareStatement(sql);
            psmpt.setString(1,aname);
            psmpt.setString(2,apwd);
            rs = psmpt.executeQuery();
            if (rs.next()){
                admin =  new Admin();
                admin.setId(rs.getInt("id"));
                admin.setAname(rs.getString("aname"));
                admin.setApwd(rs.getString("apwd"));
                admin.setPhone(rs.getString("phone"));
                admin.setEmail(rs.getString("email"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return admin;
    }

    /**
     * 添加管理员模块
     * @param admin
     */
    public void addAdmin(Admin admin){
        String sql="insert into t_admin(aname,apwd,phone,email) values(?,?,?,?)";
        try {
            psmpt = conn.prepareStatement(sql);
            psmpt.setString(1,admin.getAname());
            psmpt.setString(2,admin.getApwd());
            psmpt.setString(3,admin.getPhone());
            psmpt.setString(4,admin.getEmail());
            psmpt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     *查询所有管理员
     * @return
     */
    public List<Admin> getAllAdmin(){
        List<Admin> list = new ArrayList<>();
        String sql = "select * from t_admin";
        try {
             psmpt = conn.prepareStatement(sql);
             rs = psmpt.executeQuery();
             while(rs.next()){
                 admin = new Admin();
                 admin.setId(rs.getInt("id"));
                 admin.setAname(rs.getString("aname"));
                 admin.setPhone(rs.getString("phone"));
                 admin.setEmail(rs.getString("email"));
                 list.add(admin);
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通过用户id得到所有信息
     * @param id
     * @return
     */
    public Admin getAdminById(int id){
        String sql = "select * from t_admin where id=?";
        try {
            psmpt = conn.prepareStatement(sql);
            psmpt.setInt(1,id);
            rs = psmpt.executeQuery();
            if (rs.next()){
//                JasperException一般都是打错字母或者少写了一些语句
                admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setAname(rs.getString("aname"));
                admin.setApwd(rs.getString("apwd"));
                admin.setPhone(rs.getString("phone"));
                admin.setEmail(rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }
    /**
     * 更新用户信息
     * @param admin
     * @return
     */
    public boolean updateAdmin(Admin admin){
        boolean flag = false;
        String sql = "update t_admin set aname=?,apwd=?,phone=?,email=? where id = ?";
        try {
            psmpt = conn.prepareStatement(sql);
            psmpt.setString(1,admin.getAname());
            psmpt.setString(2,admin.getApwd());
            psmpt.setString(3,admin.getPhone());
            psmpt.setString(4,admin.getEmail());
            psmpt.setInt(5,admin.getId());
            int result = psmpt.executeUpdate();
            if (result>0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    public boolean delUpdate(int id){
        //delete 不要带*号
        String sql = "delete  from t_admin where id = ?";
        try {
            psmpt = conn.prepareStatement(sql);
            psmpt.setInt(1,id);
            int result =  psmpt.executeUpdate();
            if (result>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

}
